package library.commands;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import library.domain.Book;
import library.service.BookService;

public class SearchCommand extends Command {

    public SearchCommand(ObjectInputStream input, ObjectOutputStream output, Socket socket) {
        super(input, output, socket);
    }

    @Override
    public void execute() throws Exception {
        String type = (String) input.readObject();
        List<Book> result = new LinkedList<Book>();
        BookService bookService = serviceLocator.getService(BookService.class);
        if (type.equalsIgnoreCase("NAME")) {
            // TODO: Поиск по имени
            String name = (String) input.readObject();
            result = bookService.findByName(name);
        } else if (type.equalsIgnoreCase("TAG")) {
            // TODO: Поиск по тегу
            String tag = (String) input.readObject();
            result = bookService.findByTag(tag);
        }
        // TODO: EVERYTHING!

        /*Book b1 = new Book();
        b1.setName("Name1");
        b1.setAuthor("Author1");
        b1.setExtension("djvu");
        result.add(b1);
        b1 = new Book();
        b1.setName("Name2");
        b1.setAuthor("Author2");
        b1.setExtension("djvu");
        result.add(b1);
        b1 = new Book();
        b1.setName("Name3");
        b1.setAuthor("Author3");
        b1.setExtension("djvu");
        result.add(b1);*/
        output.writeObject(result);
    }
}
